(function($) {
	function PreLoad(imgs, options) {
		this.imgs = (typeof imgs == 'string') ? [imgs] : imgs
		this.opts = $.extend({}, PreLoad.DEFAULTS, options) //对象融合，后面覆盖前面
		if(this.opts.order == 'ordered') {
			this._ordered() //有序
		} else {
			this._unordered() //无序
		}

	};
	PreLoad.DEFAULTS = {
		order: 'unordered', //无序加载
		each: null, //每张图片加载完后执行
		all: null, //所有图片加载完后执行		
	};
	//有序加载
	PreLoad.prototype._ordered = function() {
		var ims = this.imgs
		var opts = this.opts
		var counts = 0
		var len = imgs.length
		load()
		function load() {
			var imgObj = new Image()
			if(counts >= len) {
				opts.all && opts.all(counts) //所有图片加载完成
				return
			} else {
				imgObj.src = imgs[counts] //加载图片
				$(imgObj).on('load error', function() {
					opts.each && opts.each(counts)
					counts++
					load()
				})
			}
		}
	};

	//无序加载
	PreLoad.prototype._unordered = function() { //无需加载
		var ims = this.imgs
		var opts = this.opts
		var counts = 0
		var len = imgs.length
		$.each(imgs, function(i, src) {
			if(typeof src != 'string') return;
			var imgObj = new Image();
			$(imgObj).on('load error', function() {
				opts.each && opts.each(counts)
				if(counts >= len - 1) {
					opts.all && opts.all(counts) //所图片加载完
					return
				}
				counts++
			})
			imgObj.src = src
		})
	}
	$.extend({
		preload: function(imgs, opts) {
			new PreLoad(imgs, opts)
		}
	});

})(jQuery)